package com.example.bloodbankproject;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bloodbankproject.bean.DonorBean;
import com.example.bloodbankproject.global.ExpandCollapseAnimation;
import com.example.bloodbankproject.global.GetResponse;
import com.example.bloodbankproject.global.Session;
import com.google.gson.Gson;

public class RecentUpdatesActivity  extends Activity {

	private ArrayList<DonorBean> donarList;
	private int mHeight = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recentupdates);
		
		UrlConnection conn = new UrlConnection();
		String url = Session.sInstanceUrl+"/services/data/v26.0/query/?q=select+Name,Address__c,BloodGroup__c,City__c,Donor_Age__c,Email_ID__c,Phone_Number__c,State__c,ZipCode__c,Gender__c+from+DonorRegistration__c+order+by+Date__c+DESC+LIMIT+5";
		conn.execute(url);
	}
	
	class UrlConnection extends AsyncTask<String, Void, String> {

		private HttpURLConnection http_url_conn;
		private String response;
		@Override
		protected String doInBackground(String... params) {

			try {

				URL w_Url = new URL(params[0]);
				http_url_conn = (HttpURLConnection) w_Url.openConnection();
				http_url_conn.setRequestMethod("GET");
				// headers
				http_url_conn.setRequestProperty("Content-type", "application/json");
				http_url_conn.setRequestProperty("Authorization", "OAuth "+Session.sAccessToken);

				Log.i("NCS",
						"Response code is  :" + http_url_conn.getResponseCode());
				Log.i("NCS",
						"Response Method is  :"
								+ http_url_conn.getRequestMethod());
				Log.i("NCS",
						"Response Message is  :"    
								+ http_url_conn.getResponseMessage());

				response =GetResponse.convert_To_String(http_url_conn.getInputStream());

			

				Log.i("NCS", "Response is  :" +response);

				return response;

			} catch (Exception e) {
				e.printStackTrace();
			}
			return response;
		}

		@Override
		protected void onPostExecute(String result) {
			
			if(result!=null){
				
				
				try{
					
					donarList = new ArrayList<DonorBean>();
					
				 	JSONObject  _jObject = new JSONObject(response);
			 	   	
			 	   	JSONArray _jArray = _jObject.getJSONArray("records");
			 	   	
			 	   	int count = _jArray.length();
			 	   	Gson _gson = new Gson();
			 	   	
			 	   	for(int i=0;i<count;i++){
			 	   		
			 	   		if(!_jArray.isNull(i)){
			 	   		DonorBean bean = _gson.fromJson(_jArray.getString(i), DonorBean.class);
			 	   		donarList.add(bean);
			 	   		}
			 	   	
			 	   	}
			 	   	
			 	   displayEmployeeDetails();			 	   
			 	   	
				}catch (Exception e) {
					e.printStackTrace();
				}
				
			
			}
			super.onPostExecute(result);
		}


	}
	
		
		
	
	
	private void displayEmployeeDetails(){

		LinearLayout mainLayout = (LinearLayout)findViewById(R.id.sectionsLayout);
		for (int i = 0; i < donarList.size(); i++) {

			DonorBean donor = donarList.get(i);
			View view = getLayoutInflater().inflate(R.layout.displaydetails, mainLayout,false);
			
			//View headerColor = (View)view.findViewById(R.id.title_graphic);
			//headerColor.setBackgroundResource(R.drawable.title_bar_orange);
			
			TextView	mName = (TextView) view.findViewById(R.id.tv_donorname);
			TextView	mBgroup = (TextView) view.findViewById(R.id.tv_donorbloodgroup);
			TextView 	mAge = (TextView) view.findViewById(R.id.tv_donorage);
			TextView 	mGender = (TextView) view.findViewById(R.id.tv_donorgender);
			TextView	mPhno = (TextView) view.findViewById(R.id.tv_donorphno);
			
			mName.setText(donor.getDonarName());
			mBgroup.setText(donor.getBloodgroup());
			mAge.setText(donor.getAge());
			mGender.setText(donor.getGender());
			mPhno.setText(donor.getPhone());

			mainLayout.addView(view);


			final ImageView iv_expandcollapse = (ImageView) view.findViewById(R.id.iv_CollapseExpand);
			final LinearLayout sectionitemsParent = (LinearLayout) view.findViewById(R.id.llSectionItems);
			iv_expandcollapse.setImageResource(R.drawable.collapse);
			sectionitemsParent.setVisibility(View.VISIBLE);
			

			iv_expandcollapse.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					if (sectionitemsParent.getVisibility() == View.VISIBLE) {// Collapse
						ExpandCollapseAnimation collapseAnimation = new ExpandCollapseAnimation(sectionitemsParent, 1000, 1, iv_expandcollapse);
						mHeight = collapseAnimation.getHeight();
						iv_expandcollapse.startAnimation(collapseAnimation);
					} else {// Expand
						ExpandCollapseAnimation expandAnimation = new ExpandCollapseAnimation(sectionitemsParent, 1000, 0, iv_expandcollapse);
						if (mHeight == 0) {
							mHeight = getSectionitemsHeight();
						}
						expandAnimation.setHeight(mHeight);
						iv_expandcollapse.startAnimation(expandAnimation);
					}
				}

				private int getSectionitemsHeight() {
					return (int) (4 * getResources().getDimension(R.dimen.section_item_height));
				}
			});
		}

	}
	
}
